package com.myframe.www.view.status;


import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.myframe.www.view.listener.OnPageRefreshListener;
import com.yong.android.framework.view.StatusLayout;
import com.yong.android.framework.view.adapter.ViewHolderLifecycle;


/**
 * Description:
 * Author:yong
 * Date:2017-12-21
 */

public final class DefaultStatusLayout extends StatusLayout {
    private OnPageRefreshListener mOnPageRefreshListener;

    public DefaultStatusLayout(@NonNull Context context) {
        super(context);
        setAdapter(new Adapter());
    }

    public DefaultStatusLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setAdapter(new Adapter());
    }

    public DefaultStatusLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setAdapter(new Adapter());

    }


    public void setOnPageRefreshListener(OnPageRefreshListener onPageRefreshListener) {
        this.mOnPageRefreshListener = onPageRefreshListener;
    }

    public void notifyRefresh() {
        notifyStatus(ViewConstant.VIEW_STATUS_REFRESH);
        if (mOnPageRefreshListener != null) {
            mOnPageRefreshListener.onPageRefresh();
        }
    }

    public void notifySuccess() {
        showContentView();
    }

    public void notifyFailure() {
        notifyStatus(ViewConstant.VIEW_STATUS_NETWORK_FAILURE);
    }

    private final static class Adapter extends StatusLayout.Adapter<StatusLayout.ViewHolder> {
        private SparseArray<StatusView> statusViewSparseArray = new SparseArray<>();

        public Adapter() {
            addStatusView(new NetworkFailurePageView());
            addStatusView(new DefaultEmptyPageView());
            addStatusView(new RefreshPageView());
        }

        public void addStatusView(StatusView statusView) {
            statusViewSparseArray.put(statusView.getStatus(), statusView);
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int status) {
            StatusView statusView = statusViewSparseArray.get(status);
            if (statusView != null) {
                return new DefaultStatusHolder(statusView, LayoutInflater.from(parent.getContext()), parent);
            }
            return null;

        }

        @Override
        public void onBindViewHolder(ViewHolder holder) {

        }


        @Override
        public void onViewAttachedToWindow(ViewHolder holder) {
            super.onViewAttachedToWindow(holder);
            if (holder instanceof ViewHolderLifecycle) {
                ((ViewHolderLifecycle) holder).onViewAttachedToWindow();
            }
        }

        @Override
        public void onViewDetachedFromWindow(ViewHolder holder) {
            super.onViewDetachedFromWindow(holder);
            if (holder instanceof ViewHolderLifecycle) {
                ((ViewHolderLifecycle) holder).onViewDetachedFromWindow();
            }
        }
    }

    private static final class DefaultStatusHolder extends ViewHolder implements ViewHolderLifecycle {
        private StatusView mStatusView;

        public DefaultStatusHolder(StatusView statusView, LayoutInflater inflater, ViewGroup parent) {
            super(statusView.onCreateStatusView(inflater, parent));
            mStatusView = statusView;
            mStatusView.onStatusViewCreated();
        }

        @Override
        public void onViewAttachedToWindow() {
            mStatusView.onViewAttachedToWindow();
        }

        @Override
        public void onViewDetachedFromWindow() {
            mStatusView.onViewDetachedFromWindow();
        }
    }


}
